package com.dems.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import com.dems.domain.Administrator;
import com.dems.domain.Student;
import com.dems.util.JDBCUtils;

public class AdministratorDao {
	Connection conn = null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	
	//根据账号查询id
	public Administrator selectOne(String administratorNum) {
		try {
	           conn = JDBCUtils.getConnection();
	           String sql = "select * from administrator where administrator_num=?";
	           ps = conn.prepareStatement(sql);
	           ps.setString(1,administratorNum);
	           rs = ps.executeQuery();
	           if(rs.next()){
	               Administrator administrator = new Administrator();
	               administrator.setAdministratorId(rs.getInt("administrator_id"));
	               administrator.setAdministratorNum(rs.getString("administrator_num"));
	               administrator.setAdministratorName(rs.getString("administrator_name"));
	               administrator.setAdministratorPassword(rs.getString("administrator_password"));
	               administrator.setBuildingId(rs.getString("building_id"));
	               administrator.setAdministratorPhone(rs.getString("administrator_phone"));
	               return administrator;
	           }else{
	              return null;
	           }
	       }catch (Exception e){
	           e.printStackTrace();
	           return null;
	       }finally {
	           JDBCUtils.close(conn,ps,rs);
	       }
	}
	
	//注册
	public void regist(String administratorNum,String administratorPassword) {
		try {
			conn = JDBCUtils.getConnection();
			ps = conn.prepareStatement("insert into administrator values(null,?,?)");
			ps.setString(1, administratorNum);
			ps.setString(2, administratorPassword);
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
			throw new RuntimeException(e);
		}finally{
			JDBCUtils.close(conn, ps, null);
		}
	}

    //修改admin信息
    public boolean update(Administrator administrator) {
        int i = 0;
        try {
            conn = JDBCUtils.getConnection();
            ps = conn.prepareStatement("update administrator set administrator_name=?,administrator_password=?,building_id=?,administrator_phone=? where administrator_num=?");
            ps.setString(1, administrator.getAdministratorName());
            ps.setString(2,administrator.getAdministratorPassword());
            ps.setString(3, administrator.getBuildingId());
            ps.setString(4, administrator.getAdministratorPhone());
            ps.setString(5, administrator.getAdministratorNum());
            i = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
            return i>0;
        }finally{
            JDBCUtils.close(conn, ps, null);
        }
        return i>0;
    }
	
	//管理员登录
	public Administrator login(String accountId, String password) {
		try {
            conn = JDBCUtils.getConnection();
            String sql = "select * from administrator where administrator_num=? and administrator_password=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,accountId);
            ps.setString(2,password);
            rs = ps.executeQuery();
            if(rs.next()){
                Administrator administrator = new Administrator();
                administrator.setAdministratorId(rs.getInt("administrator_id"));
                administrator.setAdministratorNum(rs.getString("administrator_num"));
                administrator.setAdministratorName(rs.getString("administrator_name"));
                administrator.setAdministratorPassword(rs.getString("administrator_password"));
                administrator.setBuildingId(rs.getString("building_id"));
                administrator.setAdministratorPhone(rs.getString("administrator_phone"));
                return administrator;
            }else{
               return null;
            }
        }catch (Exception e){
            e.printStackTrace();
            return null;
        }finally {
            JDBCUtils.close(conn,ps,rs);
        }
	}


	
	//忘记密码
	public boolean forget(String accountId, String phone, String password) {
		int i = 0;
		try {
            conn = JDBCUtils.getConnection();
            String sql = "update administrator set administrator_password=? where administrator_num=? and administrator_phone=?";
            ps = conn.prepareStatement(sql);
            ps.setString(1,password);
            ps.setString(2,accountId);
            ps.setString(3, phone);
            i = ps.executeUpdate();
        }catch (Exception e){
            e.printStackTrace();
            return i > 0;
        }finally {
            JDBCUtils.close(conn,ps,rs);
        }
		return i > 0;
	}
	
}
